package com.example.its2021606134chh.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.example.its2021606134chh.ThresholdModule.ThresholdParams;
import com.google.gson.Gson;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TMragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TMragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private TextView tvTMDate;
    private TextView tvDMCarNum;
    private Switch switchTMcar1;
    private Switch switchTMcar2;
    private Switch switchTMcar3;
    private ImageView imgTMLight;
    private Date fTime;
    private Timer timer;
    private TimerTask timerTask;
    private int num = -1;

    public TMragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TMragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TMragment newInstance(String param1, String param2) {
        TMragment fragment = new TMragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_t_mragment, container, false);
        tvTMDate = (TextView) view.findViewById(R.id.tvTMDate);
        tvDMCarNum = (TextView) view.findViewById(R.id.tvDMCarNum);
        switchTMcar1 = (Switch) view.findViewById(R.id.switchTMcar1);
        switchTMcar2 = (Switch) view.findViewById(R.id.switchTMcar2);
        switchTMcar3 = (Switch) view.findViewById(R.id.switchTMcar3);
        imgTMLight = (ImageView) view.findViewById(R.id.imgTMLight);
//         默认显示今日日期以及对应的出行车辆号码
        Date dNow = new Date();
        SimpleDateFormat ft1 = new SimpleDateFormat ("yyyy年M月d日");
        String sNow = ft1.format(dNow);
        tvTMDate.setText(sNow);
        SimpleDateFormat ft2 = new SimpleDateFormat ("d");
        String sNowDay = ft2.format(dNow);
        int nowDay = Integer.parseInt(sNowDay);
        initDate(nowDay);
        // Inflate the layout for this fragment

//        点击日期显示区域，弹出日期对话框，设定新日期后，界面单双号车辆管制功能随之而变
        tvTMDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
                View view2 = LayoutInflater.from(Objects.requireNonNull(getActivity()).getBaseContext()).inflate(R.layout.activity_date_dialog,null,false);
                dialog.setView(view2);
                TextView tvDateCancel = (TextView)view2.findViewById(R.id.tvDateCancel);
                TextView tvDateSave = (TextView)view2.findViewById(R.id.tvDateSave);
                Spinner spYear = (Spinner)view2.findViewById(R.id.spYear);
                Spinner spMonth = (Spinner)view2.findViewById(R.id.spMonth);
                Spinner spDay = (Spinner)view2.findViewById(R.id.spDay);
                TextView tvDialYear = (TextView)view2.findViewById(R.id.tvDialYear);
                TextView tvDialMon = (TextView)view2.findViewById(R.id.tvDialMon);
                TextView tvDialDay = (TextView)view2.findViewById(R.id.tvDialDay);
                tvDialYear.setText(spYear.getSelectedItem().toString());
                tvDialMon.setText(spMonth.getSelectedItem().toString());
                tvDialDay.setText(spDay.getSelectedItem().toString());
                spYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        tvDialYear.setText(spYear.getSelectedItem().toString());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                spMonth.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        tvDialMon.setText(spMonth.getSelectedItem().toString());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });
                spDay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        tvDialDay.setText(spDay.getSelectedItem().toString());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                AlertDialog mdialog = dialog.show();

                tvDateSave.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String sYear = spYear.getSelectedItem().toString();
                        String sMonth = spMonth.getSelectedItem().toString();
                        String sDay = spDay.getSelectedItem().toString();
                        String sTime = sYear + "-" + sMonth + "-" + sDay;
                        fTime = stringToDate(sTime);
                        SimpleDateFormat ft1 = new SimpleDateFormat ("yyyy年M月d日");
                        String sNow = ft1.format(fTime);
                        tvTMDate.setText(sNow);
                        SimpleDateFormat ft2 = new SimpleDateFormat ("d");
                        String sNowDay = ft2.format(fTime);
                        int nowDay = Integer.parseInt(sNowDay);
                        initDate(nowDay);
                        mdialog.dismiss();
                    }

                    private Date stringToDate(String dateString) {
                        ParsePosition position = new ParsePosition(0);
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        return simpleDateFormat.parse(dateString,position); //将字符串转换为Date日期格式
                    }
                });

                tvDateCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mdialog.dismiss();
                    }
                });
            }
        });

//        动态显示红黄绿灯顺序动画
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (num == -1) {
                            imgTMLight.setImageResource(R.drawable.hongdeng01);
                            num++;
                        } else if (num == 0) {
                            imgTMLight.setImageResource(R.drawable.huangdeng02);
                            num++;
                        } else if (num == 1) {
                            imgTMLight.setImageResource(R.drawable.lvdeng03);
                            num = -1;
                        }
                    }
                });
            }
        };
        timer.schedule(timerTask, 0, 5000);
        return view;
    }

//    switch控件设置
    private void initDate(int nowDay) {
        if (isOdd(nowDay)) {
            tvDMCarNum.setText("单数出行车辆：1、3");
            switchTMcar1.setText("开");
            switchTMcar1.setChecked(true);
            switchTMcar1.setEnabled(true);
            switchTMcar3.setText("开");
            switchTMcar3.setChecked(true);
            switchTMcar3.setEnabled(true);
            switchTMcar2.setText("停");
            switchTMcar2.setChecked(false);
            switchTMcar2.setEnabled(false);
        } else {
            tvDMCarNum.setText("双数出行车辆：2");
            switchTMcar2.setText("开");
            switchTMcar2.setChecked(true);
            switchTMcar2.setEnabled(true);
            switchTMcar1.setText("停");
            switchTMcar1.setChecked(false);
            switchTMcar1.setEnabled(false);
            switchTMcar3.setText("停");
            switchTMcar3.setChecked(false);
            switchTMcar3.setEnabled(false);
        }
    }
    @Override
    public void onPause() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask =  null;
        }
        super.onPause();
    }
//    奇偶判断
    public boolean isOdd(int number)
    {
        return number % 2 == 1;
    }
}